package com.ypinkj.app.api;


import com.ypinkj.app.annotation.UserLoginToken;
import com.ypinkj.app.entity.Industry;
import com.ypinkj.app.entity.TreeDto;
import com.ypinkj.app.mapper.IndustryMapper;
import com.ypinkj.app.service.IndustryService;
import com.ypinkj.app.utils.TreeToolUtils;
import com.ypinkj.app.utils.resultutils.Result;
import com.ypinkj.app.utils.resultutils.ResultUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.annotation.Resources;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LF
 * @since 2020-10-13
 */
@RestController
@RequestMapping("/industry")
public class IndustryController {


    /*行业*/
    @Autowired
    private IndustryService industryService;


    //拦截访问
    @UserLoginToken
    @RequestMapping(value = "/getAll")
    public  Object getAll(){
        System.out.println("获取了全部信息");
        return  industryService.fineTree();
    }

   // @UserLoginToken
    @RequestMapping(value = "/getTree")
    public Result reLoadTree(){
        ArrayList<TreeDto> rootList = new ArrayList<>();//根节点
        ArrayList<TreeDto> bodyList = new ArrayList<>();//子节点



        List<Industry> industryList = industryService.fineTree();


        for (int i = 0; i < industryList.size(); i++) {
            TreeDto treeDto = new TreeDto(
                    industryList.get(i).getIndustryId().toString(),
                    industryList.get(i).getIndustryName(),
                    industryList.get(i).getParentId().toString(),
                    "true",null
            );
            bodyList.add(treeDto);
        }




        TreeDto treeDto = new TreeDto("0", "行业信息", "null", "true",null);

        rootList.add(treeDto);

        TreeToolUtils utils =  new TreeToolUtils(rootList,bodyList);


        List<TreeDto> result =  utils.getTree();

        return  ResultUtil.success(result);
    }

}
